package com.example.online_order.conrtoller;

import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.Result;
import com.example.online_order.pojo.Shoppingcar;
import com.example.online_order.service.AdminAcountService;
import com.example.online_order.service.ShoppingcarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shoppingcar")
@CrossOrigin
@Slf4j
public class ShoppingcarController {
//    增加，删除，修改（修改数量，修改总价，总价不支持用户修改，用户修改菜品数量自动更新）,全表查询

    @Autowired
    ShoppingcarService shoppingcarService;
    @GetMapping("/getAllProducts")
    public Result getAllProducts(){
        return new Result(shoppingcarService.getAllProducts());
    }

    @GetMapping("/getProductById/{id}")
    public Result getProductById(@PathVariable("id") Integer id){
        return new Result(shoppingcarService.getProductById(id));
    }

    @PostMapping("/addToCar")
    public Result addToCar(@RequestBody Shoppingcar product){
        Integer res = shoppingcarService.addToCar(product);
        if (res != null ){
            return new Result(shoppingcarService);
        }else{
            return  Result.fail("shoppingCar already",500);
        }
    }
    @DeleteMapping("/delProduct/{id}")
    public Result delProduct(@PathVariable("id") Integer id){
        Integer res = shoppingcarService.deleteProduct(id);
        if (res == 1){
            return new Result("delete success");
        }else{
            return  Result.fail("delete failed",444);
        }
    }

    @PutMapping("/updatePrice")
    public Result updatePrice(@PathVariable("id") Integer id,@PathVariable("price") String price){
        Integer res = shoppingcarService.updatePrice(id,price);
        if (res == 1){
            return new Result("update success");
        } else{
            return  Result.fail("update failed",444);
        }
    }
    @PutMapping("/updateQuantity")
    public Result updateQuantity(@PathVariable("id") Integer id,@PathVariable("goodsNum") String num){
        Integer res = shoppingcarService.updateQuantity(id,num);
        if (res == 1){
            return new Result("update success");
        } else{
            return  Result.fail("update failed",444);
        }
    }
}
